package com.taru.wxhsh.service.impl;

import com.taru.wxhsh.mapper.OtherDataMapper;
import com.taru.wxhsh.mapper.ProductMapper;
import com.taru.wxhsh.pojo.Product;
import com.taru.wxhsh.service.ProductService;
import com.taru.wxhsh.util.UidGenerators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 商品管理服务
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    OtherDataMapper  otherDataMapper;

    @Override
    public List<Product> findProductsList(String categoryId,String  productName,String isLack) {
        return productMapper.selectList( categoryId,  productName==null?null:productName.trim(), isLack);
    }

    @Override
    public List<Product> findProductsByCategory(String productCategoryId) {
        return productMapper.selectListByCategory(productCategoryId);
    }

    @Transactional()
    @Override
    public void addProduct(Product product){
        String uuid =UidGenerators.getUuid();
        product.setProductId(uuid);
        productMapper.insertSelective(product);
        List<HashMap>  maps = product.getOtherDatas();
        if(maps!=null && maps.size()>0){
            for(HashMap map:maps){
                map.put("dataProductId",uuid);
            }
            otherDataMapper.insertWithMany(product.getOtherDatas());
        }
    }

    @Override
    public void updateStatusById(String productId) {
        productMapper.updateStatusById(productId);
    }

    @Override
    public Product findProductById(String productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public List<Product> selectBasicInfoList() {
        return productMapper.selectBasicInfoList();
    }

    @Override
    public List<Product> selectHistoryProducts(String userId) {
        return productMapper.selectHistoryProducts(userId);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void updatePhoto(String productId, String photo) {
        productMapper.updatePhotoById(productId , photo);
    }

    @Override
    public List<Product> selectByLikeName(String productName) {
        return productMapper.selectByLikeName(productName);
    }

    @Override
    public int upProductOrderById(String productId) { return productMapper.upProductOrderById(productId); }

    @Override
    public int downProductOrderById(String productId) { return productMapper.downProductOrderById(productId);}

    @Override
    public int updateNoLackById(String productId) {
        return productMapper.updateNoLackById(productId);
    }

    @Override
    public int updateLackById(String productId) {
        return productMapper.updateLackById(productId);
    }


}
